package com.hmall.trade.config;

import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TODO
 *
 * @Author mr.zhao
 * @Date 2025/6/20 16:02
 */
@Configuration
public class FeignInterceptorConfig {

	@Bean
	public RequestInterceptor userInfoRequestInterceptor(){
		return new RequestInterceptor() {
			@Override
			public void apply(RequestTemplate requestTemplate) {
				//1. 获取threadlocal中的userid
				Long userId = UserContext.getUser();

				//2. 如果非空则设置到请求头中,传给被调用的微服务
				if (userId != null){
					requestTemplate.header("user-info",userId.toString());
				}
			}
		};
	}
}
